package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private AttrValueService attrValueService;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    @Transactional
    public void saveOrUpdateBySubmitVo(RoomSubmitVo roomSubmitVo) {
        if (roomSubmitVo.getId() != null) {
            removeById(roomSubmitVo.getId());
        }

        // 1、添加主体
        saveOrUpdate(roomSubmitVo);
        Long roomId = roomSubmitVo.getId();
        // 2、添加房间租期表
        List<RoomLeaseTerm> roomLeaseTermList = roomSubmitVo.getLeaseTermIds().stream().map(LeaseTermId ->
                RoomLeaseTerm.builder().leaseTermId(LeaseTermId).roomId(roomId).build()).collect(Collectors.toList());
        roomLeaseTermService.saveBatch(roomLeaseTermList);
        // 3、添加房间支付
        List<RoomPaymentType> roomPaymentTypeList = roomSubmitVo.getPaymentTypeIds().stream().map(paymentTypeId ->
                RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomId).build()).collect(Collectors.toList());
        roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        // 4、添加房间属性值
        List<RoomAttrValue> roomAttrValueList = roomSubmitVo.getAttrValueIds().stream().map(attrValueId ->
                RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomId).build()).collect(Collectors.toList());
        roomAttrValueService.saveBatch(roomAttrValueList);
        // 5、添加房间标签
        List<RoomLabel> roomlabelList = roomSubmitVo.getLabelInfoIds().stream().map(labelInfoId ->
                RoomLabel.builder().labelId(labelInfoId).roomId(roomId).build()).collect(Collectors.toList());
        roomLabelService.saveBatch(roomlabelList);
        // 6、添加房间配套
        List<RoomFacility> roomFacilityList = roomSubmitVo.getFacilityInfoIds().stream().map(facilityId ->
                RoomFacility.builder().facilityId(facilityId).roomId(roomId).build()).collect(Collectors.toList());
        roomFacilityService.saveBatch(roomFacilityList);
        // 7、添加房间图片
        List<GraphInfo> graphInfoList = roomSubmitVo.getGraphVoList().stream().map(graphVo ->
                        GraphInfo.builder().itemType(ItemType.ROOM).itemId(roomId).name(graphVo.getName()).url(graphVo.getUrl()).build())
                .collect(Collectors.toList());
        graphInfoService.saveBatch(graphInfoList);

    }

    // 删除房间
    @Override
    @Transactional
    public void removeById(Long id) {
        // 1、删除主体
        super.removeById(id);

        // 2、删除房间租期表
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
        // 3、删除房间支付
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        // 4、删除房间属性值
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        // 5、删除房间标签
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        // 6、删除房间配套
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        // 7、删除房间图片
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.ROOM));
    }

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return  baseMapper.pageItem(page,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        // 1、添加主体
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        RoomInfo roomInfo = getById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfoMapper.selectById(roomDetailVo.getApartmentId()));
        // 2、添加房间租期表
        roomDetailVo.setLeaseTermList(leaseTermService.listByRoomId(id));
        // 3、添加房间支付
        roomDetailVo.setPaymentTypeList(paymentTypeService.listByRoomId(id));
        // 4、添加房间属性值
        roomDetailVo.setAttrValueVoList(attrValueService.listByRoomId(id));
        // 5、添加房间标签
        roomDetailVo.setLabelInfoList(labelInfoService.listByRoomId(id));
        // 6、添加房间配套
        roomDetailVo.setFacilityInfoList(facilityInfoService.listByRoomId(id));
        // 7、添加房间图片

        roomDetailVo.setGraphVoList( graphInfoService.listByRoomId(id));

        return roomDetailVo;
    }
}




